import { defHttp } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { BaseResponse, PagerResponse, AbsPageQuery } from '@/api/base';

// 定义接口地址
enum Api {
  /** 批量删除 */
  BATCH_DELETE = '/authority/api/batchDelete',
  /** 删除 */
  DELETE_BY_ID = '/authority/api/delete/',
  /** 查询基本信息 */
  DETAIL = '/authority/api/',
  /** 列表查询 */
  LIST = '/authority/api/list',
  /** 分页查询 */
  PAGER = '/authority/api/pager',
  /** 新增 */
  SAVE = '/authority/api/save',
  /** 树状 */
  TREE = '/authority/api/tree',
  /** 修改 */
  UPDATE = '/authority/api/update/',
}

// 定义参数类型

export interface ApiDO {
  id?: number;
  creator?: String;
  createTime?: Date;
  updater?: String;
  updateTime?: Date;
  moduleName?: String;
  httpMethod?: String;
  requestUri?: String;
  perms?: String;
  status?: number;
  parentId?: number;
  name?: String;
  description?: String;
}

export interface ApiQuery extends AbsPageQuery{
  id?: number;
  creator?: String;
  createTime?: Date;
  updater?: String;
  updateTime?: Date;
  moduleName?: String;
  httpMethod?: String;
  requestUri?: String;
  perms?: String;
  status?: number;
  parentId?: number;
  name?: String;
  description?: String;
}

export interface ApiRequest {
  id?: number;
  creator?: String;
  createTime?: Date;
  updater?: String;
  updateTime?: Date;
  moduleName?: String;
  httpMethod?: String;
  requestUri?: String;
  perms?: String;
  status?: number;
  parentId?: number;
  name?: String;
  description?: String;
}

export interface ApiDO extends ApiDO{
  children?: ApiDO[];
}

// 定义接口方法
// 批量删除
export function batchDelete(params: number[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.BATCH_DELETE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 删除
export function deleteById(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.delete<BaseResponse<boolean>>(
    {
      url: Api.DELETE_BY_ID + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 查询基本信息
export function detail(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.get<BaseResponse<ApiDO>>(
    {
      url: Api.DETAIL + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 列表查询
export function list(params: ApiQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<ApiDO>>>(
    {
      url: Api.LIST,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 分页查询
export function pager(params: ApiQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<PagerResponse<ApiDO>>>(
    {
      url: Api.PAGER,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 新增
export function save(params: ApiRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.SAVE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 树状
export function tree(params: ApiQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<ApiDO>>>(
    {
      url: Api.TREE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 修改
export function update(id: number, params: ApiRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.put<BaseResponse<boolean>>(
    {
      url: Api.UPDATE + '/' + id,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

